﻿@{
    ViewBag.Title = "车间列表";
}
@section SideNav{@Html.Partial("SideNavPartialView")}

<ol class="breadcrumb">
    <li><span class="glyphicon glyphicon-home"></span>  @Html.ActionLink("首页", "Index", "Home")</li>
    <li class="active">@Html.ActionLink("生产管理", "Index", "Workshop")</li>
    <li class="active">@Html.ActionLink("车间信息", "Index", "Workshop")</li>
</ol>

<div class="form-inline">
    <input id="search" type="text" name="QueryString" class="form-control" placeholder="输入检索词" />
    <input type="button" id="btnSearch" value="检索" class="btn btn-default" />
</div>

<div id="toolbar" class="btn-group" role="group">
    <button id="btn_add" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span>添加</button>
</div>

<table id="workshopgrid"></table>
<script type="text/javascript">
    $('#btnSearch').click(function () {
        var $table = $('#workshopgrid');
        $table.bootstrapTable("refresh");
    });
    $(document).ready(function () {
        var $table = $('#workshopgrid');
        $table.bootstrapTable({
            toolbar: "#toolbar",
            showRefresh: true,
            showColumns: true,
            pageNumber: 1,
            pageSize: 10,
            pagination: true,
            sidePagination: "server",
            pageList: "[5, 10, 20, 50, 100]",
            method: "post",
            url: "@Url.Action("PageList")",
            queryParams: queryParams,
            queryParamsType: "limit", //参数格式,发送标准的RESTFul类型的参数请求
            columns: [
                { title: "ID", field: "WorkshopID" },
                { title: "车间名称", field: "Name" },
                { title: "制造部", field: "ManufacturingID", formatter: function (value, row, index) { return row.Manufacturing.Name } },
                { title: "子编号", field: "SubID" },
                { title: "是否禁用", field: "IsDisabled", formatter: function (value) { if (value > 0) return "<font color='#00CC33'>否</font>"; else return "<font color='#FF0000'>是</font>"; } },
                { title: "创建时间", field: "Created_at", formatter: function (value) { return moment(value).format("YYYY-MM-DD HH:mm:ss") } },
                { title: "更新时间", field: "Updated_at", formatter: function (value) { return moment(value).format("YYYY-MM-DD HH:mm:ss") }, visible: false },
                {
                    title: "操作", field: "WorkshopID", formatter: function (value) {
                        return "<a class='btn btn-sm btn-primary' href ='#' data-method='Modify' data-value=" + value + ">编辑</a> <a class='btn btn-sm btn-danger' data-method='Delete' data-value='" + value + "'>删除</a>"

                    }
                }
            ],

            onAll: function (name, args) {
                if ((name == "load-success.bs.table") || (name == "column-switch.bs.table")) {
                    //删除按钮
                    $("a[data-method='Delete']").click(function () {
                        var id = $(this).attr("data-value");
                        BootstrapDialog.confirm("确定要删除该项", function (result) {
                            if (result) {
                                $.post("@Url.Action("Delete")", { workshopid: id }, function (data) {
                                    if (data.Code == 1) {
                                        BootstrapDialog.show({
                                            message: "删除成功",
                                            buttons: [{
                                                icon: "glyphicon glyphicon-ok",
                                                label: "确定",
                                                action: function (dialogItself) {
                                                    $table.bootstrapTable("refresh");
                                                    dialogItself.close();
                                                }
                                            }]
                                        });
                                    }
                                    else BootstrapDialog.alert(data.Message);
                                }, "json");
                            }
                        });
                    });
                    //删除结束

                    //修改
                    $("a[data-method='Modify']").click(function () {
                        var id = $(this).attr("data-value");
                        var modifyDialog = new BootstrapDialog({
                            title: "<span class='glyphicon glyphicon-user'></span>修改",
                            message: function (dialog) {
                                var $message = $('<div></div>');
                                var pageToLoad = dialog.getData('pageToLoad');
                                $message.load(pageToLoad);
                                return $message;
                            },
                            data: {
                                'pageToLoad': '@Url.Action("Modify")/' + id
                            },
                            buttons: [{
                                icon: "glyphicon glyphicon-plus",
                                label: "保存",
                                action: function (dialogItself) {
                                    $.post($("form").attr("action"), $("form").serializeArray(), function (data) {
                                        if (data.Code == 1) {
                                            BootstrapDialog.show({
                                                message: data.Message,
                                                buttons: [{
                                                    icon: "glyphicon glyphicon-ok",
                                                    label: "确定",
                                                    action: function (dialogItself) {
                                                        $table.bootstrapTable("refresh");
                                                        dialogItself.close();
                                                        modifyDialog.close();
                                                    }
                                                }]

                                            });
                                        }
                                        else BootstrapDialog.alert(data.Message);
                                    }, "json");
                                    $("form").validate();
                                }
                            }, {
                                icon: "glyphicon glyphicon-remove",
                                label: "关闭",
                                action: function (dialogItself) {
                                    dialogItself.close();
                                }
                            }]
                        });
                        modifyDialog.open();
                    });
                    //修改结束
                }
            }
        });

        //添加
        $("#btn_add").click(function () {
            var addDialog = new BootstrapDialog({
                title: "<span class='glyphicon glyphicon-plus'></span>添加",
                message: function (dialog) {
                    var $message = $('<div></div>');
                    var pageToLoad = dialog.getData('pageToLoad');
                    $message.load(pageToLoad);

                    return $message;
                },
                data: {
                    'pageToLoad': '@Url.Action("Add")'
                },
                buttons: [{
                    icon: "glyphicon glyphicon-plus",
                    label: "添加",
                    action: function (dialogItself) {
                        if ($("form").valid()) {
                            $.post($("form").attr("action"), $("form").serializeArray(), function (data) {
                                if (data.Code == 1) {
                                    BootstrapDialog.show({
                                        message: data.Message,
                                        buttons: [{
                                            icon: "glyphicon glyphicon-ok",
                                            label: "确定",
                                            action: function (dialogItself) {
                                                $table.bootstrapTable("refresh");
                                                dialogItself.close();
                                                addDialog.close();
                                            }
                                        }]

                                    });
                                }
                                else BootstrapDialog.alert(data.Message);
                            }, "json");
                        }
                    }
                }, {
                    icon: "glyphicon glyphicon-remove",
                    label: "关闭",
                    action: function (dialogItself) {
                        dialogItself.close();
                    }
                }]
            });
            addDialog.open();
        });
        //添加结束

    })
</script>

<script type="text/javascript">
    function queryParams(params) {
        var temp = {
            pageNumber: params.offset,
            pageSize: params.limit,
            name: document.getElementById("search").value
        };
        return temp;
    }
</script>

